
--This function name and notes cannot be modified
--@@funCfg: <joinUs, noexist>
function joinUs()
------------USER CODE DATA--------------
lvgl.style_set_bg_color(Style_LvglButton2_1, lvgl.STATE_DEFAULT, lvgl.color_hex(0xFF0000))
lvgl.style_set_value_str(Style_LvglButton2_1, lvgl.STATE_DEFAULT, "逗你的，还是手机扫码加群一起玩耍吧")
lvgl.style_set_value_color(Style_LvglButton2_1, lvgl.STATE_DEFAULT, lvgl.color_hex(0x1A1A1A))
lvgl.obj_add_style(ScreenA.LvglButton2.self, lvgl.BTN_PART_MAIN, Style_LvglButton2_1)
------------USER CODE DATA--------------
end


--This function name and notes cannot be modified
--@@funCfg: <reset, noexist>
function reset()
------------USER CODE DATA--------------
lvgl.style_set_bg_color(Style_LvglButton2_1, lvgl.STATE_DEFAULT, lvgl.color_hex(0x0088FF))
lvgl.style_set_value_str(Style_LvglButton2_1, lvgl.STATE_DEFAULT, "Hello LuatOS UI")
lvgl.obj_add_style(ScreenA.LvglButton2.self, lvgl.BTN_PART_MAIN, Style_LvglButton2_1)
lvgl.style_set_value_color(Style_LvglButton2_1, lvgl.STATE_DEFAULT, lvgl.color_hex(0xFFFFFF))
------------USER CODE DATA--------------
end--This function name and notes cannot be modified
local numtable_init = {
    {0, 0, 0, 0},
    {3, 0, 1, 2},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
}


local numtable = {
    {0, 0, 0, 0},
    {3, 0, 1, 2},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
}

local fail = false
local finish = false
local  overstr = "游戏失败!!!!"

function checkfinish()
    finish = true
    local row_i,col_j=1,1
    print("checkfinish start!!!!")
    for row_i=1,4,1 do
        for col_j=1,4,1 do
            print(row_i,col_j,numtable[row_i][col_j])
            if numtable[row_i][col_j] == 0 then
                finish = false
                print("checkfinish row:",row_i,"col:",col_j,numtable[row_i][col_j])
                break
            end
        end       
    end

    print("checkfinish end!!!!",finish)

    if finish then 
        overstr = "闯关成功!!!!"
        print("闯关成功!!!!")
        gameover(overstr)
    end
end

function check(row,col,val)
    ----如果新值跟旧值一样，就不去做检查
    if value == numtable[row][col] then        
        return
    end

    -----check row
    for i=1,4,1 do
        if val == numtable[row][i] then
            print("check row fail,numberi:",row,i,numtable[row][i],val)
            fail = true
            break   
        end
    end

    -----check col
    if not fail then
        for i=1,4,1 do
            if val == numtable[i][col] then
                print("check col fail,numberi:",i,col,numtable[i][col],val)
                fail = true
                break   
            end
        end  
    end

    -----check rect
    if not fail then
        local frow,fcol=0,0
        if row%2 == 1 then 
            frow = row+1 
        else
            frow = row-1 
        end
        if col%2 == 1 then 
            fcol = col+1 
        else
            fcol = col-1 
        end

        if val == numtable[row][fcol] or val == numtable[frow][col] or val == numtable[frow][fcol] then
            print("check rect fail:",val,numtable[row][fcol],numtable[frow][col],numtable[frow][fcol])
            fail = true   
        end       
    end

    numtable[row][col] = val 

    print("row1",numtable[1][1],numtable[1][2],numtable[1][3],numtable[1][4])
    print("row2",numtable[2][1],numtable[2][2],numtable[2][3],numtable[2][4])
    print("row3",numtable[3][1],numtable[3][2],numtable[3][3],numtable[3][4])
    print("row4",numtable[4][1],numtable[4][2],numtable[4][3],numtable[4][4])

    if fail  then
        overstr = "游戏失败!!!!"
        print("游戏失败!!!!")
        gameover(overstr)
    else
        checkfinish()
    end
    
end

function gameover(overstr)
    lvgl.dropdown_set_selected(P1.LvglDropdown1.self,0)
    lvgl.dropdown_set_selected(P1.LvglDropdown2.self,0)
    lvgl.dropdown_set_selected(P1.LvglDropdown3.self,0)
    lvgl.dropdown_set_selected(P1.LvglDropdown4.self,0)
    lvgl.dropdown_set_selected(P1.LvglDropdown5.self,0)
    lvgl.dropdown_set_selected(P1.LvglDropdown6.self,0)
    lvgl.dropdown_set_selected(P1.LvglDropdown7.self,0)
    lvgl.dropdown_set_selected(P1.LvglDropdown8.self,0)
    lvgl.dropdown_set_selected(P1.LvglDropdown9.self,0)
    lvgl.dropdown_set_selected(P1.LvglDropdown10.self,0)
    lvgl.dropdown_set_selected(P1.LvglDropdown11.self,0)
    lvgl.dropdown_set_selected(P1.LvglDropdown12.self,0)
    lvgl.dropdown_set_selected(P1.LvglDropdown13.self,0)

    fail = false
    for i=1,4,1 do
        for j=1,4,1 do
            numtable[i][j] = numtable_init[i][j]
            print(numtable[i][j])
        end       
    end
    lvgl.obj_del(P1.contFather_P1)
	P2.create()
    lvgl.label_set_text(P2.LvglLabel3.self, overstr)
end


--@@funCfg: <handler1c1, exist>
function handler1c1()
------------USER CODE DATA--------------
local row,col=1,1

sel = lvgl.dropdown_get_selected(P1.LvglDropdown1.self)

check(row,col,sel)
------------USER CODE DATA--------------
end



--This function name and notes cannot be modified
--@@funCfg: <handler1c2, exist>
function handler1c2()
------------USER CODE DATA--------------
local row,col=1,2

sel = lvgl.dropdown_get_selected(P1.LvglDropdown2.self)

check(row,col,sel)
------------USER CODE DATA--------------
end


--This function name and notes cannot be modified
--@@funCfg: <handler1c3, exist>
function handler1c3()
------------USER CODE DATA--------------
local row,col=1,3

sel = lvgl.dropdown_get_selected(P1.LvglDropdown3.self)

check(row,col,sel)
------------USER CODE DATA--------------
end


--This function name and notes cannot be modified
--@@funCfg: <handler1c4, exist>
function handler1c4()
------------USER CODE DATA--------------
local row,col=1,4

sel = lvgl.dropdown_get_selected(P1.LvglDropdown4.self)

check(row,col,sel)
------------USER CODE DATA--------------
end


--This function name and notes cannot be modified
--@@funCfg: <handler2c2, exist>
function handler2c2()
------------USER CODE DATA--------------
local row,col=2,2

sel = lvgl.dropdown_get_selected(P1.LvglDropdown5.self)

check(row,col,sel)
------------USER CODE DATA--------------
end


--This function name and notes cannot be modified
--@@funCfg: <handler3c1, exist>
function handler3c1()
------------USER CODE DATA--------------
local row,col=3,1

sel = lvgl.dropdown_get_selected(P1.LvglDropdown6.self)

check(row,col,sel)
------------USER CODE DATA--------------
end


--This function name and notes cannot be modified
--@@funCfg: <handler3c2, exist>
function handler3c2()
------------USER CODE DATA--------------
local row,col=3,2

sel = lvgl.dropdown_get_selected(P1.LvglDropdown7.self)

check(row,col,sel)
------------USER CODE DATA--------------
end


--This function name and notes cannot be modified
--@@funCfg: <handler3c3, exist>
function handler3c3()
------------USER CODE DATA--------------
local row,col=3,3

sel = lvgl.dropdown_get_selected(P1.LvglDropdown8.self)

check(row,col,sel)
------------USER CODE DATA--------------
end


--This function name and notes cannot be modified
--@@funCfg: <handler3c4, exist>
function handler3c4()
------------USER CODE DATA--------------
local row,col=3,4

sel = lvgl.dropdown_get_selected(P1.LvglDropdown9.self)

check(row,col,sel)
------------USER CODE DATA--------------
end


--This function name and notes cannot be modified
--@@funCfg: <handler4c1, exist>
function handler4c1()
------------USER CODE DATA--------------
local row,col=4,1

sel = lvgl.dropdown_get_selected(P1.LvglDropdown10.self)

check(row,col,sel)
------------USER CODE DATA--------------
end


--This function name and notes cannot be modified
--@@funCfg: <handler4c2, exist>
function handler4c2()
------------USER CODE DATA--------------
local row,col=4,2

sel = lvgl.dropdown_get_selected(P1.LvglDropdown11.self)

check(row,col,sel)
------------USER CODE DATA--------------
end


--This function name and notes cannot be modified
--@@funCfg: <handler4c3, exist>
function handler4c3()
------------USER CODE DATA--------------
local row,col=4,3

sel = lvgl.dropdown_get_selected(P1.LvglDropdown12.self)

check(row,col,sel)
------------USER CODE DATA--------------
end


--This function name and notes cannot be modified
--@@funCfg: <handler4c4, exist>
function handler4c4()
------------USER CODE DATA--------------
local row,col=4,4

sel = lvgl.dropdown_get_selected(P1.LvglDropdown13.self)

check(row,col,sel)
------------USER CODE DATA--------------
end
